package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.constant.RedisMessageConstant;
import cn.itcast.interfaces.OrderService;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.Result;
import cn.itcast.utils.DateUtils;
import cn.itcast.utils.SMSUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

/**
 * @author: peterChen
 * @date: 2019/8/14 18:35
 * @description: TODO
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {
    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 提交体检预约数据（校验、预约）
     * @param map
     * @return
     */
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map){
        //获取手机号及验证码
        String telePhone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        //获取redis中缓存的验证码
        String redisCode = jedisPool.getResource().get(telePhone + RedisMessageConstant.SENDTYPE_ORDER);
        if(ObjectUtils.isEmpty(redisCode) || !redisCode.equalsIgnoreCase(validateCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        Result result = null;
        try {
            //校验通过，预约体检服务，确定预约类型-微信预约
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            //调用service层方法
            result = orderService.order(map);
        } catch (Exception e) {
            //e.printStackTrace();
            //预约失败
            log.error("order setmealOrder error.",e);
            return result;
        }
        //确定预约结果
        if(result.isFlag()){
            //获取预约日期
            String orderDate = (String) map.get("orderDate");
            try {
                //预约成功，发送预约成功通知短信
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telePhone,orderDate);
            } catch (ClientException e) {
                //e.printStackTrace();
                log.error("send order success message error.",e);
            }
        }
        return result;
    }


    @RequestMapping("/findById")
    public Result findById(Integer id) throws Exception {

            Map map = orderService.findById(id);
            if(map != null){
                //此时orderDate是日期类，不是指定字符串，需转换为指定字符串格式
                Date orderDdate = (Date) map.get("orderDdate");
                map.put("orderDate", DateUtils.parseDate2String(orderDdate));
                return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
            }
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
    }
}
